# -*- coding: utf-8 -*-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy  # 导入数据库链接配置库
from config import config  # 导入配置文件


db = SQLAlchemy()  # 数据库对象


def create_app(config_name):
    """工厂函数，初始化flask_app，进行相关配置导入"""
    app = Flask(__name__, static_folder='static', template_folder='template')

    # app.config['WTF_CSRF_ENABLED'] = False
    app.config.from_object(config[config_name])  # 导入配置
    # app.config['JSON_AS_ASCII'] = False  # 解决中文乱码的问题，将json数据内的中文正常显示
    config[config_name].init_app(app)
    # app.config['JSON_AS_ASCII'] = False

    # 初始化扩展
    db.init_app(app)

    # 注册路由，自定义错误页面
    from .stock import stock as admin_blueprint
    app.register_blueprint(admin_blueprint, url_prefix='/stock')

    return app



